<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="选择学员" prop="studentName">
        <student-select v-model="form.studentName" placeholder="查询学生"  @change="handleStudentChange" ></student-select>
        <template>
          <div class="red-text">按最新报名从上到下排序</div>
        </template>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>



    <el-table v-loading="loading" :data="attendList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="上课日期" align="center" prop="claDate" width="180">
        <template slot-scope="scope">
          <span >{{ scope.row.claDate }} {{ scope.row.startTime.substr(0,5) }}~{{ scope.row.endTime.substr(0,5) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="学生" align="center" prop="studentName" />
      <el-table-column label="班级" align="center" prop="claName" />
      <el-table-column label="教师" align="center" prop="teacherName" />
      <el-table-column align="center" prop="attendStatus" label="到课状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.attendStatus === '1'" size="medium">到课</el-tag>
          <el-tag v-if="scope.row.attendStatus === '2'" size="medium" type="warning">请假</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="扣减课时数量" align="center" prop="payHour" />
      <el-table-column align="center" prop="createTime" width="140" label="点名时间" show-overflow-tooltip>
        <template slot-scope="scope">
          {{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}') }}
        </template>
      </el-table-column>

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改上课出勤对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAttend, getAttend, delAttend, addAttend, updateAttend } from "@/api/system/attend";
import {getStudent} from "@/api/system/student";
import studentSelect from '@/components/Student/studentSelect'

export default {
  name: "Attend",
  components:{
    studentSelect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 上课出勤表格数据
      attendList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        studentId: null,
      },
      // 表单参数
      form: {   studentName: '',
        studentId:''},
      // 表单校验
      rules: {
        studentCourseId: [
          { required: true, message: "sc_student_course 主键不能为空", trigger: "blur" }
        ],
        courseTimeId: [
          { required: true, message: "sc_cla_time.course_time_id不能为空", trigger: "blur" }
        ],
        studentId: [
          { required: true, message: "学生不能为空", trigger: "blur" }
        ],
        claId: [
          { required: true, message: "班级 冗余自动不能为空", trigger: "blur" }
        ],
        courseId: [
          { required: true, message: "课程 冗余自动不能为空", trigger: "blur" }
        ],
        teacherId: [
          { required: true, message: "上课教师不能为空", trigger: "blur" }
        ],
        attendStatus: [
          { required: true, message: "出席状态 1:到课 2:请假 3:缺勤不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {

    handleStudentChange(val){
      if (val !== null && val !== ''){

        this.form.studentId=val
        console.log(this.form.studentId)

        getStudent(val).then(response => {
          this.form.studentName=response.data.name


        })
      }else {
        this.form.studentId=''
        this.form.studentName=''
      }



    },
    /** 查询上课出勤列表 */
    getList() {
      this.loading = true;
      listAttend(this.queryParams).then(response => {
        this.attendList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        attendId: null,
        studentCourseId: null,
        courseTimeId: null,
        studentId: null,
        claId: null,
        courseId: null,
        teacherId: null,
        teacherName: null,
        attendStatus: "0",
        payHour: null,
        createUser: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.studentId=this.form.studentId
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.attendId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加上课出勤";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const attendId = row.attendId || this.ids
      getAttend(attendId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改上课出勤";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.attendId != null) {
            updateAttend(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAttend(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const attendIds = row.attendId || this.ids;
      this.$modal.confirm('是否确认删除上课出勤编号为"' + attendIds + '"的数据项？').then(function() {
        return delAttend(attendIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/attend/export', {
        ...this.queryParams
      }, `attend_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="scss" scoped>
.red-text{
  color: red;
  position: absolute;
  top: -23px;
  left: 5px;
  font-size: 13px;
}
</style>
